package com.cdl.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

import com.cdl.utils.bean.PropertyInfo;
import com.cdl.utils.bean.TableInfo;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import freemarker.template.TemplateNotFoundException;

public class Run {
	private static final Configuration CONFIGURATION;
	private static TableInfo tableInfo ;
	static{
		/*配置*/
		CONFIGURATION=new Configuration(Configuration.VERSION_2_3_24);
		try {
			String url = System.getProperty("user.dir")+"/template";
			//String url = Run.class.getResource("./template").getPath();
			
			CONFIGURATION.setDirectoryForTemplateLoading(new File(url));
		} catch (IOException e) {
			e.printStackTrace();
		}
		CONFIGURATION.setDefaultEncoding("utf-8");
		CONFIGURATION.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
		//不记录，抛出来
		CONFIGURATION.setLogTemplateExceptions(false);
	}
	
	public Run(String tableName){
	    tableInfo = ColumnInfoUtil.getTableInfo(tableName) ;
	    if(tableInfo!=null){
	    	tableInfo.setUrl(new PropertyInfo());
	    }
	}
	
	/***
	 * 创建XXXEntity.java文件
	 * @param
	 */
	public void createEntity(){
		String fileDir = tableInfo.getUrl().getJavaRootPath()+tableInfo.getUrl().getEntityPath()+"\\";
		String fileName = tableInfo.getClassName()+".java";
		createFile(fileDir,fileName,"entity.ftl");
	}

	/***
	 * 创建XXXDo.java文件
	 * @param
	 */
	public void createDo(){
		String fileDir = tableInfo.getUrl().getJavaRootPath()+tableInfo.getUrl().getEntityPath()+"\\";
		String fileName = tableInfo.getClassName()+"Do.java";
		createFile(fileDir,fileName,"do.ftl");
	}

	/***
	 * 创建XXXDto.java文件
	 * @param
	 */
	public void createDto(){
		String fileDir = tableInfo.getUrl().getJavaRootPath()+tableInfo.getUrl().getEntityPath()+"\\";
		String fileName = tableInfo.getClassName()+"Dto.java";
		createFile(fileDir,fileName,"dto.ftl");
	}

	/***
	 * 创建XXXVo.java文件
	 * @param
	 */
	public void createVo(){
		String fileDir = tableInfo.getUrl().getJavaRootPath()+tableInfo.getUrl().getEntityPath()+"\\";
		String fileName = tableInfo.getClassName()+"Vo.java";
		createFile(fileDir,fileName,"vo.ftl");
	}


	/***
	 * 创建XXXMapper.java文件
	 * @param
	 */
	public void createMapper(){
		String fileDir = tableInfo.getUrl().getJavaRootPath()+tableInfo.getUrl().getEntityPath()+"\\";
		String fileName = tableInfo.getClassName()+"Mapper.java";
		createFile(fileDir,fileName,"mapper.ftl");
	}

	/***
	 * 创建XXXMapper.java文件
	 * @param
	 */
	public void createDao(){
		String fileDir = tableInfo.getUrl().getJavaRootPath()+tableInfo.getUrl().getDaoPath()+"\\";
		String fileName = tableInfo.getClassName()+"Mapper.java";
		createFile(fileDir,fileName,"dao.ftl");
	}
	
	
	/***
	 * 创建XXXServiceImpl.java文件
	 * @param
	 */
	public void createServiceImpl(){
		String fileDir = tableInfo.getUrl().getJavaRootPath()+tableInfo.getUrl().getServiceImplPath()+"\\";
		String fileName = tableInfo.getClassName()+"ServiceImpl.java";
		createFile(fileDir,fileName,"serviceImpl.ftl");
	}
	
	
	/***
	 * 创建XXXEntity.java文件
	 * @param
	 */
	public void createService(){
		String fileDir = tableInfo.getUrl().getJavaRootPath()+tableInfo.getUrl().getServicePath()+"\\";
		String fileName = tableInfo.getClassName()+"Service.java";
		createFile(fileDir,fileName,"service.ftl");
	}
	
	
	/***
	 * 创建XXXController.java文件
	 * @param
	 */
	public void createController(){
		String fileDir = tableInfo.getUrl().getJavaRootPath()+tableInfo.getUrl().getControllerPath()+"\\";
		String fileName = tableInfo.getClassName()+"Controller.java";
		createFile(fileDir,fileName,"controller.ftl");
	}
	
	
	/***
	 * 创建XXXEntity.java文件
	 * @param
	 */
	public void createSqlMap(){
		String fileDir = tableInfo.getUrl().getSqlMapRoot()+tableInfo.getUrl().getSqlMapPath()+"\\";
		String fileName = tableInfo.getLowerClassName()+"-mapper.xml";
		createFile(fileDir,fileName,"sqlmap.ftl");
	}


	public void createDomain(){
		String fileDir = tableInfo.getUrl().getJavaRootPath()+tableInfo.getUrl().getEntityPath()+"\\";
		String fileName = tableInfo.getClassName()+".java";
		createFile(fileDir,fileName,"domain.ftl");
	}

	public void createRequest(){
		String fileDir = tableInfo.getUrl().getJavaRootPath()+tableInfo.getUrl().getEntityPath()+"\\";
		String fileName = tableInfo.getClassName()+"Request.java";
		createFile(fileDir,fileName,"request.ftl");
	}

	public void createQueryRequest(){
		String fileDir = tableInfo.getUrl().getJavaRootPath()+tableInfo.getUrl().getEntityPath()+"\\";
		String fileName = tableInfo.getClassName()+"QueryRequest.java";
		createFile(fileDir,fileName,"queryRequest.ftl");
	}

	public void createResponse(){
		String fileDir = tableInfo.getUrl().getJavaRootPath()+tableInfo.getUrl().getEntityPath()+"\\";
		String fileName = tableInfo.getClassName()+"Response.java";
		createFile(fileDir,fileName,"response.ftl");
	}


	public void createServiceDC(){
		String fileDir = tableInfo.getUrl().getJavaRootPath()+tableInfo.getUrl().getServicePath()+"\\";
		String fileName = tableInfo.getClassName()+"Service.java";
		createFile(fileDir,fileName,"service-datacenter.ftl");
	}

	public void createServiceImplDC(){
		String fileDir = tableInfo.getUrl().getJavaRootPath()+tableInfo.getUrl().getServiceImplPath()+"\\";
		String fileName = tableInfo.getClassName()+"ServiceImpl.java";
		createFile(fileDir,fileName,"serviceImpl-datacenter.ftl");
	}

	public void createMapperDC(){
		String fileDir = tableInfo.getUrl().getJavaRootPath()+tableInfo.getUrl().getDaoPath()+"\\";
		String fileName = tableInfo.getClassName()+"Mapper.java";
		createFile(fileDir,fileName,"mapper-datacenter.ftl");
	}

	public void createsqlMapDC(){
		String fileDir = tableInfo.getUrl().getSqlMapRoot()+tableInfo.getUrl().getSqlMapPath()+"\\";
		String fileName = tableInfo.getClassName()+"Mapper.xml";
		createFile(fileDir,fileName,"sqlmap-datacenter.ftl");
	}

	public void createControllerDC(){
		String fileDir = tableInfo.getUrl().getJavaRootPath()+tableInfo.getUrl().getControllerPath()+"\\";
		String fileName = tableInfo.getClassName()+"Controller.java";
		createFile(fileDir,fileName,"controller-datacenter.ftl");
	}

	/****************************************************************/


	
	/***
	 * 
	 * 根据freemark模板生成文件
	 * @param fileDir
	 * @param fileName
	 * @param template
	 */
	public void createFile(String fileDir,String  fileName,String template){
		String filePath = fileDir+fileName;
		File dir = new File(fileDir);
		File f = new File(filePath);
		if(!dir.exists()){
			dir.mkdirs();
		}
		if(f.exists()){
			System.out.println(f.getAbsolutePath()+"文件已存在！");
			return ;
		}
		Template t;
		Writer w=null;
			try {
				t = CONFIGURATION.getTemplate(template);
				w = new OutputStreamWriter(new FileOutputStream(filePath),"utf-8");
				t.process(tableInfo, w);
				w.flush();
				System.out.println(filePath+" 创建成功！"); 
			} catch (TemplateNotFoundException e) {
				System.err.println("获取模板"+ template +"失败！");
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				System.err.println("文件路径异常：---->"+filePath);
				e.printStackTrace();
			} catch (Exception e) {
				System.err.println("生成文件失败：---->"+filePath);
				e.printStackTrace();
			}finally{
				try {
					if(w!=null){
						w.close();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
	}
	
	
	
	public static void main(String[] args) {
		System.out.println("#########################");
		System.out.println("####  自动生成代码!  ####");
		System.out.println("#########################");
		args = new String[1];
		args[0] = "biz_address";
		//args[0] = "specialty_order_express_info";
		//args[0] = "specialty_order_product_detail_info";
		//args[0] = "specialty_refund_product_detail_info";
		if(args.length<=0){
			System.out.println("未输入表名");
			return ;
		}
		String tableName = args[0];
	    Run t = new Run(tableName);
	    if(tableInfo==null){
	    	System.out.println("数据库中找不到表："+tableName);
	    	return ;
	    }
	    //System.out.println(tableInfo);
	    t.createEntity();
	    t.createDo();
	    t.createDto();
	    t.createVo();
	    t.createMapper();
	    t.createDao();
	    t.createServiceImpl();
	    t.createService();
	    t.createSqlMap();
	    t.createController();
			
	}

}
